package com.lagou.edu.service.impl;

import com.lagou.edu.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

/**
 * @author
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    private final Logger log = LoggerFactory.getLogger(EmailServiceImpl.class);
    @Autowired
    @Qualifier("lagouMail")
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String emailFrom;

    @Override
    public Boolean sendAuthCode(String email, String authCode) {
        MimeMessage mimeMailMessage;
        try {
            mimeMailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(emailFrom);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("【lagou-cloud-login】的验证码");
            mimeMessageHelper.setText("您本次的验证码为：" + authCode, true);
            javaMailSender.send(mimeMailMessage);
        } catch (Exception e) {
            log.error("邮件发送失败!", e);
            return false;
        }
        return true;
    }
}
